package com.chenxy.common.controller;

import com.baomidou.mybatisplus.extension.service.IService;
import com.chenxy.common.result.Res;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * 具备基本CRUD的 基础Controller
 * @param <S>
 * @param <D>
 */
public class CRUDbaseController<S extends IService,D> extends BaseController{

    @Resource
    S service;

    @ApiOperation(value = "save",notes = "新增",httpMethod = "POST")
    @PostMapping("baseCRUD/save")
    public Res<String> save(@RequestBody D d) {
        boolean flag = service.save(d);
        return Res.success(flag);
    }

    @ApiOperation(value = "saveBatch",notes = "批量新增",httpMethod = "POST")
    @PostMapping("baseCRUD/saveBatch")
    public Res<String> saveBatch(@RequestBody List<D> list) {
        boolean flag = service.saveBatch(list);
        return Res.success(flag);
    }

    @ApiOperation(value = "removeById",notes = "通过id删除",httpMethod = "POST")
    @PostMapping("baseCRUD/removeById")
    public Res<String> removeById(Integer id) {
        boolean flag = service.removeById(id);
        return Res.success(flag);
    }

    @ApiOperation(value = "updateById",notes = "根据id修改",httpMethod = "POST")
    @PostMapping("baseCRUD/updateById")
    public Res<String> updateById(@RequestBody D d) {
        boolean flag = service.updateById(d);
        return Res.success(flag);
    }

    @ApiOperation(value = "updateBatchById",notes = "根据id批量修改",httpMethod = "POST")
    @PostMapping("baseCRUD/updateBatchById")
    public Res<String> updateBatchById(@RequestBody List<D> list) {
        boolean flag = service.updateBatchById(list);
        return Res.success(flag);
    }

    @ApiOperation(value = "getById",notes = "根据id查询",httpMethod = "GET")
    @GetMapping("baseCRUD/getById")
    public Res<D> getById(Integer id) {
        return Res.success(service.getById(id));
    }
}
